﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace VikingArcade_v2._0
{
    public class Menu : UserInterfaceControl
    {
        private List<TextBox> menuElements;

        public Color ElementColor { get; set; }
        public Color SelectedElementColor { get; set; }

        public Menu() : base()
        {
            menuElements = new List<TextBox>();
        }

        public void AddMenuElement(TextBox menuElement)
        {
            menuElements.Add(menuElement);
            AddChild(menuElement);

            for(int i = 0; i < menuElements.Count; ++i)
            {
                menuElements[i].Position = new Point((Size.X - menuElements[i].Size.X) / 2, (i + 1)*Size.Y/(menuElements.Count + 1));
            }

            menuElement.ForeColor = ElementColor;
            menuElement.Anchor = Anchor;
        }

        public override void UpdateMouseInput(Point mousePosition, bool leftClicked, bool rightClicked)
        {
            foreach (TextBox textBox in menuElements)
            {
                textBox.ForeColor = ElementColor;
            }
            foreach (TextBox menuElement in menuElements)
            {
                if(mousePosition.X >= menuElement.AbsolutePosition.X && mousePosition.X <= menuElement.AbsolutePosition.X + menuElement.Size.X &&
                   mousePosition.Y >= menuElement.AbsolutePosition.Y && mousePosition.Y <= menuElement.AbsolutePosition.Y + menuElement.Size.Y)
                {
                    menuElement.ForeColor = SelectedElementColor;
                    if(leftClicked)
                    {
                        MenuElementClicked.Invoke(menuElement.Text);
                    }
                    return;
                }
            }
        }

        public delegate void OnElementClick(string ElementCaption);

        public event OnElementClick MenuElementClicked;

        public override IDrawer GetDrawer()
        {
            return new MenuDrawer(this);
        }
    }
}
